package bravo.xc.com.xc_bravo_android.push;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import bravo.xc.com.xc_bravo_android.beans.SendCodeBeans;
import bravo.xc.com.xc_bravo_android.operation.login.LoginActivity;
import bravo.xc.com.xc_bravo_android.operation.login.LoginContract;
import bravo.xc.com.xc_bravo_android.operation.login.LoginPresenter;
import bravo.xc.com.xc_bravo_android.operation.person_center.PersonalCenterActivity;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.service.JPushMessageReceiver;

import static android.content.Context.MODE_PRIVATE;
import static bravo.xc.com.xc_bravo_android.beans.AliaBeans.LOGIN;
import static bravo.xc.com.xc_bravo_android.beans.AliaBeans.LOGINOUT;
import static bravo.xc.com.xc_bravo_android.beans.AliaBeans.LOGINOUTREAL;
import static bravo.xc.com.xc_bravo_android.beans.AliaBeans.LOGINREAL;
import static bravo.xc.com.xc_bravo_android.operation.login.LoginActivity.loginBeans;
import static bravo.xc.com.xc_bravo_android.operation.login.LoginActivity.loginPresenter;
import static bravo.xc.com.xc_bravo_android.operation.person_center.PersonalCenterActivity.centerPresenter;
import static bravo.xc.com.xc_bravo_android.operation.person_center.PersonalCenterActivity.sendCodeBeans;

/**
 * CREATED BY YANK
 * DATE : 2017/11/7
 * MAIL : YANK.TENYOND@GMAIL.COM
 * FUNCTION :
 */

public class MyPushReceiver extends JPushMessageReceiver {

    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onAliasOperatorResult(context, jPushMessage);
        switch (jPushMessage.getSequence()){
            case LOGIN:
                if (jPushMessage.getErrorCode() ==0){
                    Log.e("@@dead",String.valueOf(jPushMessage.getErrorCode()));
                    loginPresenter.doLogin(loginBeans,context);

                   /* context.getSharedPreferences("isLogin",MODE_PRIVATE).edit().putBoolean("flag",false).commit();
                    context.getSharedPreferences("UserInfo",MODE_PRIVATE).edit().clear();
                    Intent intent = new Intent(context, LoginActivity.class);
                    context.startActivity(intent);
                    Toast.makeText(context,"初始化失败，请重新登录",Toast.LENGTH_SHORT).show();*/
                }else if (jPushMessage.getErrorCode() !=0){
                    Log.e("@@getErrorCode",String.valueOf(jPushMessage.getErrorCode()));
                    if (jPushMessage.getErrorCode() != 0){
                        JPushInterface.setAlias(context,LOGIN,context.getSharedPreferences("username",MODE_PRIVATE).getString("username",null));
                    }

                }
                break;
            case LOGINOUT:
                if (jPushMessage.getErrorCode() ==0){
                    Log.e("@@deleteAlias","deleteAliasOK");
                }else if (jPushMessage.getErrorCode() !=0){
                    Log.e("@@getErrorCode",String.valueOf(jPushMessage.getErrorCode()));
                    if (jPushMessage.getErrorCode() != 0){
                        JPushInterface.deleteAlias(context,LOGINOUT);
                    }
                }
                /*if (jPushMessage.getErrorCode() !=0){
                    Toast.makeText(context,"注销失败，服务器错误",Toast.LENGTH_SHORT).show();
                }else if (jPushMessage.getErrorCode() ==0) {
                    Toast.makeText(context,"操作成功",Toast.LENGTH_SHORT).show();
                }*/
                break;
            case LOGINOUTREAL:
                if (jPushMessage.getErrorCode() ==0){
                    centerPresenter.logout(sendCodeBeans,context);
                }else if (jPushMessage.getErrorCode() !=0){
                    Log.e("@@getErrorCode",String.valueOf(jPushMessage.getErrorCode()));
                    if (jPushMessage.getErrorCode() != 0){
                        JPushInterface.deleteAlias(context,LOGINOUT);
                    }
                }
                /*if (jPushMessage.getErrorCode() !=0){
                    Toast.makeText(context,"注销失败，服务器错误",Toast.LENGTH_SHORT).show();
                }else if (jPushMessage.getErrorCode() ==0) {
                    Toast.makeText(context,"操作成功",Toast.LENGTH_SHORT).show();
                }*/
                break;
            case LOGINREAL:
                if (jPushMessage.getErrorCode() ==0){
                    Log.e("@@setAlias","setAliasOK");
                }else if (jPushMessage.getErrorCode() !=0){
                    Log.e("@@getErrorCode",String.valueOf(jPushMessage.getErrorCode()));
                    if (jPushMessage.getErrorCode() != 0){
                        JPushInterface.setAlias(context,LOGINOUTREAL,context.getSharedPreferences("username", Context.MODE_PRIVATE).getString("username",null));
                    }
                }
                break;

        }
    }
}
